import sortedcontainers


class Solution(object):
    def maxTwoEvents(self, events):
        d = sortedcontainers.SortedDict()
        events.sort(key=lambda x: (x[1], x[0]))
        max_val = 0
        for s, e, v in events:
            last_index = d.bisect_right(s - 1)
            insert_index = d.bisect_right(e)
            now_val = v
            if last_index == 0:
                max_val = max(max_val, now_val)
            else:
                max_val = max(max_val, now_val + d.peekitem(last_index - 1)[1])
            if insert_index == 0:
                d[e] = now_val
            else:
                last_item = d.peekitem(insert_index - 1)
                if last_item[0] == e:
                    d[e] = max(d[e], now_val)
                else:
                    if now_val > last_item[1]:
                        d[e] = now_val
        return max_val